package com.wangsh.usersCenter.users.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.wangsh.usersCenter.common.pojo.ApiResponse;
import com.wangsh.usersCenter.common.pojo.ApiResponseEnum;
import com.wangsh.usersCenter.common.service.BaseServiceImpl;
import com.wangsh.usersCenter.common.util.ConstatFinalUtil;
import com.wangsh.usersCenter.system.pojo.ASysLog;
import com.wangsh.usersCenter.system.pojo.ASysLogEnum;
import com.wangsh.usersCenter.system.service.ISystemDbService;
import com.wangsh.usersCenter.users.pojo.AAdmins;
import com.wangsh.usersCenter.users.pojo.AUsers;
import com.wangsh.usersCenter.users.pojo.AUsersEnum;
import com.wangsh.usersCenter.users.pojo.AUsersExtend;
import com.wangsh.usersCenter.users.service.INoLoginOperService;
import com.wangsh.usersCenter.users.service.IUsersDbService;
import com.wangsh.usersCenter.util.ServiceCallableUtil;

/**
 * 用户不需要登陆的一些操作
 * 
 * @author wangsh
 */
@Service("noLoginOperService")
public class NoLoginOperServiceImpl extends BaseServiceImpl implements INoLoginOperService
{
	@Resource
	private IUsersDbService usersDbService;
	@Resource
	private ISystemDbService systemDbService ; 

	public ApiResponse<Object> operLoginSubmitService(Map<String, Object> paramsMap)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--loginSubmit--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);

		/* 接收参数 */
		String email = paramsMap.get("email") + "";
		String password = paramsMap.get("password") + "";
		String code = paramsMap.get("code") + "";
		String randSess = paramsMap.get("randSess") + "";
		String failedCount = paramsMap.get("failedCount") + "";

		if ((!"".equalsIgnoreCase(failedCount) && !"null".equalsIgnoreCase(failedCount)
				&& Integer.valueOf(failedCount) > ConstatFinalUtil.CONFIG_JSON.getIntValue("session.randFailedCount"))
				|| this.regexUtil.verifyCode(randSess, code))
		{
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AUsers> apiRes = this.usersDbService.findOneUsersService(condMap);
			AUsers users = apiRes.getDataOneJava();
			/* 检验邮箱 */
			if (users != null)
			{
				/* 将邮箱放到request中 */
				dataMapJava.put("users", users);

				/* 检验密码 */
				if (this.encryptUtil.checkStr(password, users.getPassword()))
				{
					users.setSouPass(password);
					/* 状态必须为启用 */
					if (users.getStatus() == AUsersEnum.STATUS_ENABLE.getStatus())
					{
						/* 登陆成功 */
						dataMapJava.put("users", users);
						dataMapJava.put("lastLoginTime", users.getLastLoginTime());

						/* 修改管理员操作 */
						users.setLoginCount(users.getLoginCount() + 1);
						users.setLastLoginTime(new Date());

						/* 输入正确的时候,要重置 */
						users.setFailedCount(0);
						/* 不需要更新用户的扩展信息 */
						users.setUsersExtend(null);
						this.usersDbService.updateOneUsersService(users);

						apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
						apiResponse.setInfo(
								ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
								Collections.EMPTY_MAP);
						return apiResponse;
					} else
					{
						/* 状态为禁用 */
						apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus());
						apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(
								ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus() + ""), Collections.EMPTY_MAP);
					}
				} else
				{
					/* 密码不正确 */
					apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
					apiResponse.setInfo(
							ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
							Collections.EMPTY_MAP);
				}
			} else
			{
				/* 邮箱不存在 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		} else
		{
			/* 验证码正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		}
		/* 提示信息 */
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operAdminsLoginSubmitService(Map<String, Object> paramsMap)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--loginSubmit--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);

		/* 接收参数 */
		String email = paramsMap.get("email") + "";
		String password = paramsMap.get("password") + "";
		String code = paramsMap.get("code") + "";
		String randSess = paramsMap.get("randSess") + "";
		/* 获取当前登陆的ip */
		String ip = paramsMap.get("ip") + "";
		// 如果登录失败次数不为空字符串也不为null而且大于3次或者验证码验证正确
		if (this.regexUtil.verifyCode(randSess, code))
		{
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AAdmins> apiRes = this.usersDbService.findOneAdminsService(condMap);
			AAdmins admins = apiRes.getDataOneJava();
			if (admins==null)
			{
				/* 邮箱不存在 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
						Collections.EMPTY_MAP);
				return apiResponse;
			}
			int failedCount = admins.getFailedCount();
			/* 检验邮箱 */
			if (admins != null)
			{
				/* 将邮箱放到request中 */
				dataMapJava.put("admins", admins);
				/* 密码输错5次 */
				if (failedCount >=5)
				{	
					
					Date failedTime = admins.getFailedTime();
					/* 获取当前时间与账号冻结那一刻时间的时间差 */
					long timeDifference = this.dateFormatUtil.getTimeDifferenceMinute(failedTime);
					long lockTime = Long.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("password.error.lockTime"));
					lockTime=lockTime/60/1000;
					if (timeDifference >= lockTime)
					{
						failedCount=0;
					}else {
						/* 登录失败超过5次 */
						int minutes = Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("password.error.lockTime"))/1000/60;
						failedTime.setMinutes(failedTime.getMinutes()+minutes);
						dataMapJava.put("adminsEmail", admins.getEmail());
						long timeDifferenceSecond = this.dateFormatUtil.getTimeDifferenceSecond(failedTime);
						apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
						apiResponse.setInfo("五次"+timeDifferenceSecond+"",Collections.EMPTY_MAP);
						return apiResponse;
					}
				}
				/* 检验密码 */
				if (this.encryptUtil.checkStr(password, admins.getPassword()))
				{
					/* 状态必须为启用 */
					if (admins.getStatus() == AUsersEnum.STATUS_ENABLE.getStatus())
					{
						/* 登陆成功 */
						admins.setLastLoginTime(new Date());

						/* 修改管理员操作 */
						admins.setLoginCount(admins.getLoginCount() + 1);
						/* 输入正确的时候,要重置 */
						admins.setFailedCount(0);
						this.usersDbService.updateOneAdminsService(admins);
						
						/* 添加一条登陆成功的日志 */
						ASysLog sysLog = new ASysLog();
						sysLog.setIp(ip);
						sysLog.setName("登陆成功");
						sysLog.setRelaId(admins.getId());
						sysLog.setContent(admins.getName() + "-" + sysLog.getName());
						sysLog.setLogType(ASysLogEnum.LOGTYPE_ADMINS.getStatus());
						sysLog.setStatus(ASysLogEnum.STATUS_ENABLE.getStatus());
						sysLog.setCreateTime(new Date());
						sysLog.setUpdateTime(new Date());
						sysLog.setPubTime(new Date());
						/* 保存一条日志 */
						ApiResponse<Object> sysLogApiResponse = this.systemDbService.saveOneLogService(sysLog);
						ConstatFinalUtil.SYS_LOGGER.info("保存日志的结果:{}",sysLogApiResponse.toJSON().toJSONString());

						apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
						apiResponse.setInfo(
								ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
								Collections.EMPTY_MAP);
						return apiResponse;
					} else
					{
						/* 状态为禁用 */
						apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus());
						apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(
								ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus() + ""), Collections.EMPTY_MAP);
					}
				} else
				{

					/* 密码不正确 */
					dataMapJava.put("adminsEmail", admins.getEmail());
					admins.setFailedCount(failedCount + 1);
					admins.setFailedTime(new Date());
					this.usersDbService.updateOneAdminsService(admins);
					apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
					apiResponse.setInfo(
							ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
							Collections.EMPTY_MAP);
				}
			} 
		} else
		{
			/* 验证码不正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		}
		/* 提示信息 */
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operRegisterSubmitService(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		ConstatFinalUtil.SYS_LOGGER.info("--registerSubmit--");
		/* 接收参数 */
		String email = paramsMap.get("email") + "";
		String password = paramsMap.get("password") + "";
		String code = paramsMap.get("code") + "";
		String randSess = paramsMap.get("randSess") + "";

		if (this.regexUtil.verifyCode(randSess, code))
		{
			/* 创建对象 */
			AUsers users = new AUsers();
			/* 赋值 */
			users.setEmail(email);
			users.setPassword(this.encryptUtil.encodeStr(password));
			/* 设置状态 */
			users.setStatus(AUsersEnum.STATUS_ENABLE.getStatus());

			users.setCreateTime(new Date());
			users.setUpdateTime(new Date());
			users.setLastLoginTime(new Date());
			users.setSouPass(password);
			
			apiResponse = this.usersDbService.saveOneUsersService(users);
			ConstatFinalUtil.SYS_LOGGER.info("==注册成功=={}", apiResponse.toJSON());
			if (apiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
			{
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_REGISTER_SUCCESS.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		} else
		{
			/* 验证码正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operEmailVerfySendService(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		/* 根据邮箱来操作 */
		String email = paramsMap.get("email") + "";
		String contextPath = paramsMap.get("contextPath") + "";

		if (!"".equalsIgnoreCase(email))
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
			AUsers users = apiResponseRes.getDataOneJava();
			if (users != null)
			{
				/* 邮箱必须是未验证 */
				if (users.getEmailStatus() != AUsersEnum.EMAILSTATUS_PASS.getStatus())
				{
					AUsersExtend usersExtend = users.getUsersExtend();
					usersExtend.setSendEmailTime(new Date());
					String uuid = UUID.randomUUID().toString();
					/* 加密字符串: db存储为:sha256(邮箱+时间+私钥) */
					String souStr = email + uuid;
					ConstatFinalUtil.SYS_LOGGER.info("--sourceSode--:{}", souStr);
					souStr = DigestUtils.sha256Hex(souStr);
					usersExtend.setSendEmailCode(uuid);

					/* 发送邮件 */
					Map<String, String> emailMap = new HashMap<String, String>();
					emailMap.put("date", this.dateFormatUtil.dateTimeStr(new Date()));
					/* 邮箱验证的url */
					emailMap.put("verifyHref", ConstatFinalUtil.CONFIG_JSON.getString("website.url")
							+ "/emailVerfy.htm?code=" + souStr + "&email=" + users.getEmail());
					emailMap.put("websiteurl", ConstatFinalUtil.CONFIG_JSON.getString("website.url"));

					String subject = "邮箱认证 - 用户中心";
					emailMap.put("email", users.getEmail());
					emailMap.put("subject", subject);

					InputStream is = ConstatFinalUtil.class.getResourceAsStream("/template/email/emailVerify.html");
					String content = this.fileUtil.readFile(is, ConstatFinalUtil.CHARSET, emailMap);

					List<Callable<Object>> callList = new ArrayList<Callable<Object>>();
					paramsMap.clear();
					/* 发送邮件 */
					paramsMap.put("springEmailUtil", springEmailUtil);
					paramsMap.put("email", emailMap.get("email") + "");
					paramsMap.put("subject", subject);
					paramsMap.put("content", content);

					/* 多线程发送邮件 */
					ServiceCallableUtil multiThreadUtil = new ServiceCallableUtil();
					multiThreadUtil.setParamsMap(paramsMap);
					multiThreadUtil.setOperType(ServiceCallableUtil.OPERTYPE_SEND_EMAIL);
					callList.add(multiThreadUtil);

					// 固定设置线程数为
					ExecutorService exeSvc = Executors.newFixedThreadPool(callList.size());
					try
					{
						exeSvc.submit(multiThreadUtil);
					} catch (Exception e)
					{
						ConstatFinalUtil.SYS_LOGGER.error("线程池报错了", e);
					} finally
					{
						exeSvc.shutdown();
					}

					ApiResponse apiRes = this.usersDbService.updateOneUsersExtendService(usersExtend);
					ConstatFinalUtil.SYS_LOGGER.info("邮箱验证:发送邮件,db执行结果:{}", apiRes.toJSON());
					/* 成功 */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_SENDED.getStatus());

					/* 提示信息 */
					Map<String, String> infoMap = new HashMap<String, String>();
					infoMap.put("contextPath", contextPath);
					infoMap.put("email", email);
					apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);
					return apiResponse;
				} else
				{
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_PASSED.getStatus());
				}
			} else
			{
				/* 17:邮箱未注册 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			}
		} else
		{
			/* 邮箱不能为空 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NO_EMPTY.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operEmailVerfyService(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		String email = paramsMap.get("email") + "";
		String code = paramsMap.get("code") + "";

		if (!"".equalsIgnoreCase(email))
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
			AUsers users = apiResponseRes.getDataOneJava();
			if (users != null)
			{
				boolean flag = true;
				AUsersExtend usersExtend = users.getUsersExtend();
				Calendar calendar = Calendar.getInstance();
				String uuid = usersExtend.getSendEmailCode();

				/* 加密字符串: db存储为:sha256(邮箱+时间+私钥) */
				String souStr = email + uuid;
				ConstatFinalUtil.SYS_LOGGER.info("--sourceSode--:{}", souStr);
				souStr = DigestUtils.sha256Hex(souStr);

				if ((calendar.getTimeInMillis() - usersExtend.getSendEmailTime().getTime() > ConstatFinalUtil.SECOND
						* 60 * 60 * 24))
				{
					/* 验证邮箱时间 */
					/* info = "邮箱链接已经超过一天" ; */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_VERIY_HREF_TIMEOUT.getStatus());
					flag = false;
				} else if (!souStr.equalsIgnoreCase(code))
				{
					/* 验证邮件的合法性 */
					/* info = "邮箱链接已经被篡改" ; */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_VERIY_HREF_NOTSEC.getStatus());
					flag = false;
				} else if (users.getEmailStatus() == AUsersEnum.EMAILSTATUS_PASS.getStatus())
				{
					/* 验证邮箱是否被验证过 */
					flag = false;
					/* info = "邮箱已经认证" ; */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_PASSED.getStatus());
				}

				if (flag)
				{
					usersExtend.setSendEmailTime(new Date());
					/* 加密字符串: db存储为:sha256(邮箱+时间+私钥) */
					uuid = UUID.randomUUID().toString();
					usersExtend.setSendEmailCode(uuid);

					/* 修改邮箱状态 */
					users.setEmailStatus(AUsersEnum.EMAILSTATUS_PASS.getStatus());
					usersExtend.setUpdateTime(new Date());
					ApiResponse<Object> apiRes = this.usersDbService.updateOneUsersService(users);
					ConstatFinalUtil.SYS_LOGGER.info("邮箱验证结果:邮箱:{},db执行结果:{}", email, apiRes.toJSON());
					/* 成功 */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_VERIFY_SUCCESS.getStatus());
				}
			} else
			{
				/* 17:邮箱未注册 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			}
		} else
		{
			/* 邮箱不能为空 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NO_EMPTY.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operForgetPassSubmit(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		/* 根据邮箱来操作 */
		String email = paramsMap.get("email") + "";
		String code = paramsMap.get("code") + "";
		String randSess = paramsMap.get("randSess") + "";
		String contextPath = paramsMap.get("contextPath") + "";

		if (this.regexUtil.verifyCode(randSess, code))
		{
			if (!"".equalsIgnoreCase(email))
			{
				Map<String, Object> condMap = new HashMap<String, Object>();
				condMap.put("email", email);
				ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
				AUsers users = apiResponseRes.getDataOneJava();
				if (users != null)
				{
					AUsersExtend usersExtend = users.getUsersExtend();
					usersExtend.setSendEmailTime(new Date());

					String uuid = UUID.randomUUID().toString();
					/* 加密字符串: db存储为:sha256(邮箱+时间+私钥) */
					String souStr = email + uuid;
					souStr = DigestUtils.sha256Hex(souStr);
					usersExtend.setSendEmailCode(uuid);

					/* 发送邮件 */
					Map<String, String> emailMap = new HashMap<String, String>();
					emailMap.put("date", this.dateFormatUtil.dateTimeStr(new Date()));
					/* 邮箱验证的url */
					
					emailMap.put("verifyHref", ConstatFinalUtil.CONFIG_JSON.getString("website.url")
								+ "resetPass.htm?code=" + souStr + "&email=" + users.getEmail());
					emailMap.put("websiteurl", ConstatFinalUtil.CONFIG_JSON.getString("website.url"));
					
					String subject = "重置密码 - 用户中心 ";
					emailMap.put("email", users.getEmail());
					emailMap.put("subject", subject);

					InputStream is = ConstatFinalUtil.class.getResourceAsStream("/template/email/resetPass.html");
					String content = this.fileUtil.readFile(is, ConstatFinalUtil.CHARSET, emailMap);

					List<Callable<Object>> callList = new ArrayList<Callable<Object>>();

					paramsMap.clear();
					paramsMap.put("springEmailUtil", springEmailUtil);
					paramsMap.put("email", emailMap.get("email") + "");
					paramsMap.put("subject", subject);
					paramsMap.put("content", content);

					ServiceCallableUtil multiThreadUtil = new ServiceCallableUtil();
					multiThreadUtil.setParamsMap(paramsMap);
					multiThreadUtil.setOperType("sendEmail");
					callList.add(multiThreadUtil);

					// 固定设置线程数为
					ExecutorService exeSvc = Executors.newFixedThreadPool(callList.size());
					try
					{
						exeSvc.submit(multiThreadUtil);
					} catch (Exception e)
					{
						ConstatFinalUtil.SYS_LOGGER.error("线程池报错了", e);
					} finally
					{
						exeSvc.shutdown();
					}

					ApiResponse<Object> apiRes = this.usersDbService.updateOneUsersExtendService(usersExtend);
					ConstatFinalUtil.SYS_LOGGER.info("邮箱验证结果:邮箱:{},db执行结果:{}", email, apiRes.toJSON());

					/* 成功 */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_SENDED.getStatus());

					/* 提示信息 */
					Map<String, String> infoMap = new HashMap<String, String>();
					infoMap.put("contextPath", contextPath);
					infoMap.put("email", email);
					apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);
					return apiResponse;
				} else
				{
					/* info = "邮箱不存在" ; */
					/* 5:邮箱不存在 */
					/* info = ConstatFinalUtil.INFO_JSON.getString("5") ; */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
				}
			} else
			{
				/* 16:邮箱不能为空 */
				/* info = ConstatFinalUtil.INFO_JSON.getString("16") ; */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NO_EMPTY.getStatus());
			}
		} else
		{
			/* 验证码正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operResetPass(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		/* 根据邮箱来操作 */
		String email = paramsMap.get("email") + "";
		String code = paramsMap.get("code") + "";

		if (!"".equalsIgnoreCase(email))
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
			AUsers users = apiResponseRes.getDataOneJava();

			if (users != null)
			{
				boolean flag = true;
				AUsersExtend usersExtend = users.getUsersExtend();
				Calendar calendar = Calendar.getInstance();
				String uuid = usersExtend.getSendEmailCode();
				/* 加密字符串: db存储为:sha256(邮箱+时间+私钥) */
				String souStr = email + uuid;
				souStr = DigestUtils.sha256Hex(souStr);

				if ((calendar.getTimeInMillis() - usersExtend.getSendEmailTime().getTime() > ConstatFinalUtil.SECOND
						* 60 * 60 * 24))
				{
					/* 验证邮箱时间 */
					/* info = ConstatFinalUtil.INFO_JSON.getString("19"); */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_VERIY_HREF_TIMEOUT.getStatus());
					flag = false;
				} else if (!souStr.equalsIgnoreCase(code))
				{
					/* 验证邮件的合法性 */
					/* info = "邮箱链接已经被篡改" ; */
					/* info = ConstatFinalUtil.INFO_JSON.getString("20"); */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_VERIY_HREF_NOTSEC.getStatus());
					flag = false;
				}

				if (flag)
				{
					apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
				}
			} else
			{
				/* info = "邮箱不存在" ; */
				/* 5:邮箱不存在 */
				/* info = ConstatFinalUtil.INFO_JSON.getString("5") ; */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			}
		} else
		{
			/* 16:邮箱不能为空 */
			/* info = ConstatFinalUtil.INFO_JSON.getString("16") ; */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NO_EMPTY.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operResetPassSubmit(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		/* 根据邮箱来操作 */
		String email = paramsMap.get("email") + "";
		String password = paramsMap.get("password") + "";
		String randSess = paramsMap.get("randSess") + "";
		String code = paramsMap.get("code") + "";
		if (this.regexUtil.verifyCode(randSess, code))
		{
			if (!"".equalsIgnoreCase(email))
			{
				Map<String, Object> condMap = new HashMap<String, Object>();
				condMap.put("email", email);
				ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
				AUsers users = apiResponseRes.getDataOneJava();
				if (users != null)
				{
					boolean flag = true;
					users.setPassword(this.encryptUtil.encodeStr(password));
					users.setSouPass(password);
					if (flag)
					{
						AUsersExtend usersExtend = users.getUsersExtend();
						usersExtend.setSendEmailTime(new Date());
						String uuid = UUID.randomUUID().toString();
						usersExtend.setSendEmailCode(uuid);
						usersExtend.setUpdateTime(new Date());
							
						ApiResponse<Object> apiRes = this.usersDbService.updateOneUsersService(users);
						ConstatFinalUtil.SYS_LOGGER.info("密码重置成功:邮箱:{},db执行结果:{}", email, apiRes.toJSON());
						/* 成功 */
						apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
						apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
								Collections.EMPTY_MAP);
						return apiResponse;
					}
				} else
				{
					/* 17:邮箱未注册 */
					apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
				}
			} else
			{
				/* 邮箱不能为空 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NO_EMPTY.getStatus());
			}
		} else
		{
			/* 验证码正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AUsers> operAddUsersTokenService(AUsers users)
	{
		ApiResponse<AUsers> apiResponse = new ApiResponse<AUsers>();
		try
		{
			String token = users.getTokenStr();
			if (token == null || "".equalsIgnoreCase(users.getTokenStr()))
			{
				token = UUID.randomUUID().toString();
				users.setTokenStr(token);
			}
			/* 将token添加到redis中 */
			String key = redisUtil.KEY_USERS_TOKEN + users.getTokenStr();
			boolean flag = this.redisUtil.put(key, users.toJSON().toJSONString(),
					Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("timeout.token")));
			if (flag)
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("存储Redis时失败了,", e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AAdmins> operAddAdminsTokenService(AAdmins admins)
	{
		ApiResponse<AAdmins> apiResponse = new ApiResponse<AAdmins>();
		try
		{
			String token = admins.getTokenStr();
			if (token == null || "".equalsIgnoreCase(admins.getTokenStr()))
			{
				token = UUID.randomUUID().toString();
				admins.setTokenStr(token);
			}
			/* 将token添加到redis中 */
			String key = redisUtil.KEY_ADMINS_TOKEN + admins.getTokenStr();
			boolean flag = this.redisUtil.put(key, admins.toJSON().toJSONString(),
					Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("timeout.token")));
			if (flag)
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("存储Redis时失败了,", e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	public ApiResponse<Object> thawAccountService(Map<String, Object> paramsMap)
	{
		return null;
	}

}
